const app = getApp()
const { envList } = require('../../envList')
Page({
  data: {
    GzrMoney: 0,
    JjrMoney: 0,
    XxrMoney: 0
  },

  onLoad: function (options) {
    this.setData({
      GzrMoney: app.globalData.GzrMoney,
      JjrMoney: app.globalData.JjrMoney,
      XxrMoney: app.globalData.XxrMoney
    })
  },

  SaveWages() {
    const Collection = wx.cloud.database({
      env: envList[1].envId
    }).collection('UserConfig');
    Collection.where({
      _openid: app.globalData.openId
    }).get().then(res => {
      if (res.data.length > 0) {
        console.log(res)
        console.log(this.data.JjrMoney)
        console.log(app.globalData.JjrMoney)
        Collection.doc(res.data[0]._id).update({
          data: {
            GzrMoney: this.data.GzrMoney,
            JjrMoney: this.data.JjrMoney,
            XxrMoney: this.data.XxrMoney
          }
        })
      } else {
        Collection.add({
          data: {
            CycleId: 0,
            GzrMoney: this.data.GzrMoney,
            JjrMoney: this.data.JjrMoney,
            XxrMoney: this.data.XxrMoney
          }
        })
      }
      app.globalData.GzrMoney = this.data.GzrMoney;
      app.globalData.JjrMoney = this.data.JjrMoney;
      app.globalData.XxrMoney = this.data.XxrMoney;
      wx.showToast({
        title: '保存成功',
        icon: 'success',
        duration: 1000
      })
    })
  },

  inputFocus: function (e) {
    switch (e.target.id) {
      case 'gzr':
        this.setData({
          'gzr_focus': true
        }); break;
      case 'xxr':
        this.setData({
          'xxr_focus': true
        }); break;
      case 'jjr':
        this.setData({
          'jjr_focus': true
        }); break;
    }
  },
  inputBlur: function (e) {
    switch (e.target.id) {
      case 'gzr':
        this.setData({
          'gzr_focus': false,
          GzrMoney: isNaN(parseFloat(e.detail.value)) ? 0 : parseFloat(e.detail.value)
        }); break;
      case 'xxr':
        this.setData({
          'xxr_focus': false,
          XxrMoney: isNaN(parseFloat(e.detail.value)) ? 0 : parseFloat(e.detail.value)
        }); break;
      case 'jjr':
        this.setData({
          'jjr_focus': false,
          JjrMoney: isNaN(parseFloat(e.detail.value)) ? 0 : parseFloat(e.detail.value)
        }); break;
    }
  }

})